<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket test</title>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:8080/ws");

        socket.onmessage = function (ev) {
            document.getElementById("responseText").value = document.getElementById("responseText").value + "\n" + ev.data;
        }
        socket.onopen = function (ev) {
            document.getElementById("responseText").value = "连接打开...";
        }
        socket.onclose = function (ev) {
            document.getElementById("responseText").value = document.getElementById("responseText").value + "\n" + "连接关闭...";
        }
    } else {
        alert("不支持websocket");
    }

    function submitMsg() {
        if (!window.WebSocket) {
            return;
        }
        var msg = document.getElementById("message").value;

        if (socket.readyState == WebSocket.OPEN) {
            socket.send(msg);
        }

    }

</script>


<form onsubmit="return false;">
    <textarea id="message" style="width: 400px;height: 200px"></textarea>
    <input type="button" value="发送数据" onclick="submitMsg()"/>
    <h3>服务端输出</h3>
    <textarea id="responseText" style="width: 400px;height: 200px"></textarea>
    <input type="button" value="清空数据" onclick="javascript:document.getElementById('responseText').value=''"/>
</form>


</body>
</html>